package priv.menghan.happy.learning.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import priv.menghan.happy.learning.common.rsp.Response;

/**
 * 基础控制器（统一返回 Response<T>）
 *
 * @author Monika
 */
public abstract class BaseController<Req, Resp, ServiceType extends BaseService<Req, Resp>> {

	protected final ServiceType service;

	public BaseController(ServiceType service) {
		this.service = service;
	}

	@Operation(summary = "新增", description = "根据请求体创建新记录")
	@PostMapping("/create")
	public Response<Void> create(@Validated @RequestBody Req req) {
		service.create(req);
		return Response.success(null);
	}

	@Operation(summary = "编辑", description = "根据请求体更新已有记录")
	@PostMapping("/update")
	public Response<Void> update(@Validated @RequestBody Req req) {
		service.update(req);
		return Response.success(null);
	}

	@Operation(summary = "删除", description = "根据ID删除指定记录")
	@GetMapping("/del/{id}")
	public Response<Void> del(
			@Parameter(description = "目标记录的ID", example = "1")
			@PathVariable("id") Long id) {
		service.del(id);
		return Response.success(null);
	}

	@Operation(summary = "查询", description = "根据请求体分页查询记录")
	@PostMapping("/qry")
	public Response<IPage<Resp>> qry(@Validated @RequestBody Req req) {
		return Response.success(service.qry(req));
	}

	@Operation(summary = "查询", description = "根据ID获取详细信息")
	@GetMapping("/getDetail/{id}")
	public Response<Resp> getDetail(
			@Parameter(description = "目标记录的ID", example = "1")
			@PathVariable("id") Long id) {
		return Response.success(service.getDetail(id));
	}
}
